package io.renren.modules.cards.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import io.renren.common.utils.*;
import io.renren.modules.cards.dao.AmgCdkCardsDao;
import io.renren.modules.cards.entity.AmgCdkCardsEntity;
import io.renren.modules.cards.service.AmgCdkCardsService;
import io.renren.modules.sys.entity.SysDictionariesEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;


@Service("amgCdkCardsService")
public class AmgCdkCardsServiceImpl extends ServiceImpl<AmgCdkCardsDao, AmgCdkCardsEntity> implements AmgCdkCardsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        EntityWrapper<AmgCdkCardsEntity> entityWrapper = new EntityWrapper<>();
        String cdkey= (String) params.getOrDefault("cdkey","");
        String remark= (String) params.getOrDefault("remark","");
        String status= (String) params.getOrDefault("status","");
        String cdkType= (String) params.getOrDefault("cdkType","");
        String cdkDateType= (String) params.getOrDefault("cdkDateType","");
        String activatedStatus= (String) params.getOrDefault("activatedStatus","");
        String agenticState= (String) params.getOrDefault("agenticState","");
        String startDate= (String) params.getOrDefault("startDate","");
        String endDate= (String) params.getOrDefault("endDate","");
        String agentId= (String) params.getOrDefault("agentId","");
        entityWrapper.where("1=1");
        if (!StringUtils.isNull(cdkey)) {
            entityWrapper.andNew("CDKEY={0}", cdkey);
        }
        if (!StringUtils.isNull(remark)) {
            entityWrapper.andNew("REMARK={0}", remark);
        }
        if (!StringUtils.isNull(status)) {
            entityWrapper.andNew("STATUS={0}", status);
        }
        if (!StringUtils.isNull(cdkType)) {
            entityWrapper.andNew("CDK_TYPE={0}", cdkType);
        }
        if (!StringUtils.isNull(cdkDateType)) {
            entityWrapper.andNew("CDK_DATE_TYPE={0}", cdkDateType);
        }
        if (!StringUtils.isNull(activatedStatus)) {
            entityWrapper.andNew("ACTIVATED_STATUS={0}", activatedStatus);
        }
        if (!StringUtils.isNull(agenticState)) {
            entityWrapper.andNew("AGENTIC_STATE={0}", agenticState);
        }
        if(!StringUtils.isNull(agentId)){
            entityWrapper.andNew("AGENT_ID",agentId);
        }
        if (!StringUtils.isNull(startDate)) {
            entityWrapper.gt("CREATED_DATE",startDate);
        }
        if (!StringUtils.isNull(endDate)) {
            entityWrapper.lt("CREATED_DATE", endDate);
        }
        entityWrapper.orderBy("CREATED_DATE", false);
        Page<AmgCdkCardsEntity> page = this.selectPage(
                new Query<AmgCdkCardsEntity>(params).getPage(),entityWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public boolean batchAdd(Map<String, Object> params) {
        int num= Integer.parseInt((String)params.get("cdkeynum"));
        List<AmgCdkCardsEntity> list=new ArrayList<>();
        for (int i = 0; i < num; i++) {
            AmgCdkCardsEntity cards = new AmgCdkCardsEntity();
            String cdkey = UUID.randomUUID().toString().trim().replaceAll("-", "");
            cards.setCdkey(cdkey.toUpperCase());
            SysUserEntity userEntity=  (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
            cards.setCreatedBy(userEntity.getUsername());
            cards.setCreatedDate(new Date());
            cards.setCdkType((String) params.get("cdkType"));
            cards.setCdkDateType(Integer.parseInt((String) params.get("cdkDateType")));
            list.add(cards);
        }
        return    this.insertBatch(list);
    }

}
